/* ----------------------------------------------------------------------------------------------------------------------------------------------- /

MapdataOutputer - 类名称
修改许可权限 - 修改人名称

< --- Public Vars ------------------------------------------------------------------------------------------------------------------------------ >

< --- Public Functions ------------------------------------------------------------------------------------------------------------------------ >

※ 构造函数
public function MapdataOutputer()

/ ----------------------------------------------------------------------------------------------------------------------------------------------- */
package editor.map.dataOutput{
	import com.adobe.images.PNGEncoder;
	
	import editor.common.Common;
	import editor.manager.MapManger;
	import editor.map.TileLayer;
	import editor.map.elements.AvatarNpc;
	import editor.map.elements.Npc;
	import editor.map.elements.Teleport;
	import editor.ui.Alert;
	
	import flash.display.Bitmap;
	import flash.display.BitmapData;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.filesystem.File;
	import flash.filesystem.FileMode;
	import flash.filesystem.FileStream;
	import flash.geom.Rectangle;
	import flash.net.FileReference;
	import flash.net.FileReferenceList;
	import flash.utils.ByteArray;
	
	public class MapdataOutputer{
		
		public static var png:ByteArray = null;
		public static var mapData:String = null;
		
		/**
		 * 用bitmap的形式输出tile节点信息
		 * @param dataVector 存储节点的二维数组 
		 */		
		public static function outputTileBitmapdata(dataVector:Array):void
		{
			var dataXml:String = outPutXmlFile();
			if(dataXml == null) return ;
			var width:int = TileLayer.MAP_LOGIC_WIDTH;
			var height:int = TileLayer.MAP_LOGIC_HEIGHT;
			var bmp:BitmapData = new BitmapData(width, height);
			for(var i:int = 0; i < width; i ++)
			{
				for(var j:int = 0; j < height; j ++)
				{
					if(dataVector[i][j])
						bmp.setPixel(i,j,0xFFFFFF);
					else
						bmp.setPixel(i,j,0x000000);
				}
			}

			png = PNGEncoder.encode(bmp);
			mapData = dataXml;
			
			var file:File = new File();
			file.browseForDirectory("寻找文件保存路径");
			file.addEventListener(Event.SELECT, onSave);
		}
		
		private static function onSave(evt:Event):void
		{
			var file:File = evt.target as File;
			var fileStream:FileStream = new FileStream();
			var pngFile:File = file.resolvePath("map_"+Common.map.gmap.mapId+"/res/" + "path.png");
			
			fileStream.open(pngFile, FileMode.WRITE);
			fileStream.writeBytes(png);
			fileStream.close();
			
			var xmlFile:File = file.resolvePath("map_"+Common.map.gmap.mapId+"/" + Common.map.gmap.mapId + ".xml");
			fileStream.open(xmlFile, FileMode.WRITE);
			fileStream.writeUTFBytes(new XML(mapData).toXMLString());
			fileStream.close();
			
			try{
				var bgtargetFile:File = file.resolvePath("map_"+Common.map.gmap.mapId+"/" + Common.map.gmap.mapId + "_bg.png");
				Common.map.backgroundLayer.bgFile.copyTo(bgtargetFile, true);
			}catch(e:Error){}
			
			try{
				var distantTarget:File = file.resolvePath("map_"+Common.map.gmap.mapId+"/res/distant.jpg");
				Common.ui.outputWindow.distantFile.copyTo(distantTarget, true);
			}catch(e:Error){}
		}
		
		/**
		 * 输出地图的xml文件 
		 */		
		public static function outPutXmlFile():String
		{
			var npcList:Vector.<AvatarNpc> = Common.map.npcLayer.npcList;
			var mapName:String = Common.map.gmap.mapName;
			var mapId:String = Common.map.gmap.mapId;
			
			var inGate:Teleport = Common.map.npcLayer.inGate;
			var bgbmd:BitmapData = Common.map.backgroundLayer.bgBitmapdata;
			var preGate:Teleport = Common.map.npcLayer.preGate;
			var nextGate:Teleport = Common.map.npcLayer.nextGate;
			
			if(Common.ui.outputWindow.fbCheckBox == false && (inGate == null || bgbmd == null))
			{
				if(bgbmd == null) Alert.showLog("背景大图没载入");
				else Alert.showLog("请设定好进城位置");
			}
			else
			{
				var sceneData:String = '<map> \n<scene id="'+ mapId +'" name="'+ mapName +'" width="'+ bgbmd.width +'" height="'+ bgbmd.height +'" pieceW="'+ Common.pieceWidth +'" pieceH="'+Common.pieceHeight+'" > \n';
				if(inGate != null)
					sceneData += '<gate kind="in" standX="'+int((inGate.startCircle.x + inGate.x)/16)+'" standY="'+int((inGate.startCircle.y + inGate.y)/16)+'" x="'+inGate.x+'" y="'+inGate.y+'" /> \n';
				if(preGate != null)
					sceneData += '<gate kind="pre" standX="'+int((preGate.startCircle.x+preGate.x)/16)+'" standY="'+int((preGate.startCircle.y+preGate.y)/16)+'" toScene="'+preGate.toId+'" x="'+preGate.x+'" y="'+preGate.y+'" /> \n';
				if(nextGate != null)
					sceneData += '<gate kind="next" standX="'+int((nextGate.startCircle.x + nextGate.x)/16)+'" standY="'+int((nextGate.startCircle.y+nextGate.y)/16)+'" toScene="'+nextGate.toId+'" x="'+nextGate.x+'" y="'+nextGate.y+'" /> \n';
				
				for(var i:int = 0; i < npcList.length; i ++)
				{
					if(npcList[i] is AvatarNpc)
					{
						var npc:AvatarNpc = npcList[i];
						sceneData += '<npc id="'+npc.npcId+'" x="'+npc.x+'" y="'+npc.y+'" > \n';
						for each(var pot:Sprite in npc.standPoints)
						{
							sceneData +='<point x="'+ int((pot.x + npc.x)/16) + '" y="'+ int((pot.y + npc.y)/16)+'" /> \n'
						}
						sceneData += '</npc>\n'
					}
				}
				
				sceneData += "</scene> \n</map>";
				trace(sceneData);
				return sceneData;
				
			}
			return null;
		}
		
		public static function readTileBitmapdata(data:BitmapData):Array
		{
			var dataArray:Array = new Array();
			for(var i:int = 0; i < data.width; i ++)
			{
				dataArray[i] = new Array();
				for(var j:int = 0; j < data.height; j ++)
				{
					if(data.getPixel(i,j) == 0xFFFFFF)
						dataArray[i][j] = true;
					else
						dataArray[i][j] = false;
				}
			}
			
			return dataArray;
		}
		//
	}
}